package acwing._2LanQiao._10Other;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._10Other
 * @File : Q1234.java
 * @Author : WangRuoyu
 * @Date : 2023/3/26 11:15
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Q1234{
    static int n, m;
    static ArrayList<Integer>[] a;
    static int[][] f;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");

        n = Integer.parseInt(words[0]);
        m = Integer.parseInt(words[1]);
        a = new ArrayList[m];
        f = new int[4][m];

        words = br.readLine().split(" ");
        for (int i = 0; i < m; ++i) {
            a[i] = new ArrayList<>();
        }
        for (int i = 0; i < n; ++i) {
            int x = Integer.parseInt(words[i]);
            a[x % m].add(x);
        }
        for (int i = 0; i < m; ++i) {
            a[i].sort((o1, o2) -> o2 - o1);
        }
        for (int i = 0; i <= 3; i++) Arrays.fill(f[i], Integer.MIN_VALUE);
        f[0][0] = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < a[i].size() && j < 3; ++j) {
                int x = a[i].get(j);
                for (int k = 3; k >= 1; --k) {
                    for (int l = 0; l < m; ++l) {
                        f[k][l] = Math.max(f[k][l], f[k - 1][(l - x % m + m) % m] + x);
                    }
                }
            }
        }
        System.out.println(f[3][0]);
    }
}
